<?php
class ctrl_register extends ctrl_base
{
    public static function index() {
    	self::smarty() -> display('register_index.html');
    }
    //普通用户注册
    public static function reg() {
    	self::smarty()->assign("checkUserRegister",CHECK_USER_REGISTER_URL);
    	self::smarty() -> display('register.html');
    }
    //判断用户是否注册过
    public static function checkUserRegister() {
    	$User_Account = trim($_POST['User_Account']);
    	$counts = model_user::is_exists_account($User_Account);
    	if($counts > 0) {
    		$result = array(
    			'errCode' => '01',
    			'msg' => '该用户账号已存在，请换一个！'
    		);
    	} else {
    		$result = array(
    			'errCode' => '00',
    			'msg' => '可以注册'
    		);
    	}
    	framework_static_function::output_json($result);
    }
    //内部用户注册
    public static function reg2() {
    	$user_types = model_user::get_user_category_list(1);
    	self::smarty()->assign('user_types',$user_types);
    	self::smarty() -> display('register2.html');
    }
    
    public static function regSave() {
    	//$_SESSION['wx_userinfo']['openid'] = 'o-w6St-fdCFF7LTT8pOlKx6Jx4xY';
    	//获取open_id 走用户体系取 user_id
    	if(empty($_SESSION['wx_userinfo']['openid'])) {
    		setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
    		ctrl_weixin::oauth();
    	}else{
    		setcookie('backurl','');
    	}
    	$data = array();
    	$data['openid'] = $_SESSION['wx_userinfo']['openid'];
    	$data['User_Account'] = trim($_POST['User_Account']);
    	if(empty($data['User_Account'])) {
    		$errMsg = '用户账号不为空!';
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",$errMsg);
    		self::smarty()->display('show_result.html');
    		exit;
    		
    	}
    	$data['User_Password'] = sha1(trim($_POST['User_Password']));
    	if(empty($data['User_Password'])) { 
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",'账号密码不为空');
    		self::smarty()->display('show_result.html');
    		exit;
    	
    	}
    	$re_pwd = sha1(trim($_POST['User_Confirm_Password']));
    	if($data['User_Password'] != $re_pwd) {
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",'两次输入的密码不一致');
    		self::smarty()->display('show_result.html');
    		exit;
    	}
    	
    	$data['Company_Name'] = trim($_POST['Company_Name']);
    	$data['Company_Head'] = trim($_POST['Company_Head']);
    	$data['Company_Address'] = trim($_POST['Company_Address']);
    	$data['Company_Head_Tel'] = trim($_POST['Company_Head_Tel']);
    	$data['Contact_Name'] = trim($_POST['Contact_Name']);
    	$data['Contact_Dep'] = trim($_POST['Contact_Dep']);
    	$data['Contact_Job'] = trim($_POST['Contact_Job']);
    	$data['Contact_Mobile'] = trim($_POST['Contact_Mobile']);
    	$data['Contact_Tel'] = trim($_POST['Contact_Tel']);
    	$data['Contact_Fax'] = trim($_POST['Contact_Fax']);
    	$data['Contact_Mail'] = trim($_POST['Contact_Mail']);
    	
    	$data['Org_Code'] = trim($_POST['Org_Code']);
    	$data['Org_License_No'] = trim($_POST['Org_License_No']);
    	$data['Org_Capital'] = trim($_POST['Org_Capital']);
    	$data['Org_Type'] = $_POST['Org_Type'];
    	$data['Org_Business'] = trim($_POST['Org_Business']);
    	$data['Org_Product'] = trim($_POST['Org_Product']);
    	
    	//将外部用户注册保存到微信数据库
    	$user_id = model_user::add_register_user($data,'weixin_register_user');
    	if($user_id > 0) {//注册成功
    		self::smarty()->assign("result_class",'icon67_status');
    		self::smarty()->assign("errMsg",'用户注册信息提交成功！');
    		self::smarty()->assign("comment",'请返回服务号提交其他验证材料！');
    		self::smarty()->display('show_result.html');
    		$msg = "\t您好，您的信息已提交，请继续拍照上传以下验证材料：\r\n\t1.营业执照副本；\r\n\t2.组织机构代码证；\r\n\t
3.开户证明；\r\n\t4.联络人照片；\r\n\t上述信息通过认证后，方可成为普通用户，验证为我公司已合作用户的，将自动为您升级为星级用户！";
    		model_send::send_text($_SESSION['wx_userinfo']['openid'],$msg);
    		exit;
    	} else {
    		$userInfo = model_user::is_openid_registered($data['openid'],'weixin_register_user');
    		if(is_array($userInfo)&&!empty($userInfo['openid'])) {
    			if($userInfo['Deal_State'] == 0) { //待审核
    				$msg = '您已提交过注册，正在审核中...';
    			} elseif ($userInfo['Deal_State'] == 1) {  //用户已注册，且已通过审核
    				$msg = '您用户账号【'.$userInfo['User_Account'].'】注册过了，无须再注册';
    			} else {	//已注册，且审核未通过或者被软删除用户
    				$rowCount = model_user::update_weixin_register_user_info($data, $data['openid'],'weixin_register_user'); //重新注册
    				if($rowCount > 0) {
    					self::smarty()->assign("result_class",'icon67_status');
			    		self::smarty()->assign("errMsg",'用户注册信息提交成功！');
			    		self::smarty()->assign("comment",'请返回服务号提交其他验证材料！');
			    		self::smarty()->display('show_result.html');
			    		$msg = "\t您好，您的信息已提交，请继续拍照上传以下验证材料：\r\n\t1.营业执照副本；\r\n\t2.组织机构代码证；\r\n\t
			3.开户证明；\r\n\t4.联络人照片；\r\n\t上述信息通过认证后，方可成为普通用户，验证为我公司已合作用户的，将自动为您升级为星级用户！";
			    		model_send::send_text($_SESSION['wx_userinfo']['openid'],$msg);
			    		exit;
    				} else {
    					$msg = '系统异常，请稍后重试！';
    				}    				
    			}
    			self::smarty()->assign("result_class",'icon67_status warn');
    			self::smarty()->assign("errMsg",'对不起，注册失败！');
    			self::smarty()->assign("comment",$msg);
    			self::smarty()->display('show_result.html');
    			exit;
    		}
    		self::smarty()->display('error/wx_error.html'); exit;
    	}
    	
    	
    	/*$url = REGISTER_URL;
    	$postData['Data'] = json_encode($data);
    	$res = model_weixin::http_post($url,$postData);
    	$postData['Data']['User_Password']='****';//删除密码隐私数据
    	framework_static_function::write_log('用户注册数据:'.json_encode($postData),'weixin_register');
    	framework_static_function::write_log('注册接口返回:'.$res,'weixin_register');
    	if($res != false) {
    		$result = json_decode($res,true);
    		if($result['2_Status'] == 1) { //注册成功
    				self::smarty()->assign("result_class",'icon67_status');
    				self::smarty()->assign("errMsg",'用户注册信息提交成功！');
    				self::smarty()->assign("comment",'请返回服务号提交其他验证材料！');
    				self::smarty()->display('show_result.html');
    				$msg = "\t您好，您的信息已提交，请继续拍照上传以下验证材料：\r\n\t1.营业执照副本；\r\n\t2.组织机构代码证；\r\n\t
3.开户证明；\r\n\t4.联络人照片；\r\n\t上述信息通过认证后，方可成为普通用户，验证为我公司已合作用户的，将自动为您升级为星级用户！";
    				model_send::send_text($_SESSION['wx_userinfo']['openid'],$msg);
    				exit;
    		} else {
    			self::smarty()->assign("result_class",'icon67_status warn');
    			self::smarty()->assign("errMsg",'对不起，注册失败！');
    			self::smarty()->assign("comment",$result['3_Msg']);
    			self::smarty()->display('show_result.html');
    			exit;
    		}
    	} else {
    		self::smarty()->display('error/wx_error.html'); exit;
    	}*/
    }
    /**
     * 内部用户注册
     */
    public static function regSave2() {
    	//$_SESSION['wx_userinfo']['openid'] = 'o-w6St-fdCFF7LTT8pOlKx6Jx4xY';
    	//获取open_id 走用户体系取 user_id
    	if(empty($_SESSION['wx_userinfo']['openid'])) {
    		setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
    		ctrl_weixin::oauth();
    	}else{
    		setcookie('backurl','');
    	}
    	$data = array();
    	$data['openid'] = $_SESSION['wx_userinfo']['openid'];
    	$data['sales_name'] = trim($_POST['sales_name']);
    	if(empty($data['sales_name'])) {
    		$errMsg = '姓名不为空!';
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",$errMsg);
    		self::smarty()->display('show_result.html');
    		exit;
    		
    	}
    	$data['depart'] = trim($_POST['depart']);
    	if(empty($data['depart'])) { 
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",'所在部门不为空');
    		self::smarty()->display('show_result.html');
    		exit;
    	
    	}
    	    	
    	$data['sex'] = trim($_POST['sex']);
    	$data['sales_type'] = trim($_POST['sales_type']);
    	$data['sales_produce'] = trim($_POST['sales_produce']);
    	$data['sales_area'] = trim($_POST['sales_area']);
    	$data['mobile'] = trim($_POST['mobile']);
    	$data['tel'] = trim($_POST['tel']);
    	$data['email'] = trim($_POST['email']);
    	$data['fax'] = trim($_POST['fax']);
    	$data['office_addr'] = trim($_POST['office_addr']);
    	$data['user_category_id'] = trim($_POST['user_category_id']);
    	
    	
    	$user_id = model_user::add_register_user($data,'weixin_sales_user');
    	if($user_id > 0) {//注册成功
    		self::smarty()->assign("result_class",'icon67_status');
    		self::smarty()->assign("errMsg",'用户注册信息提交成功！');
    		self::smarty()->assign("comment",'等待系统管理员审核！');
    		self::smarty()->display('show_result.html');
    		exit;
    	} else {
    		$userInfo = model_user::is_openid_registered($data['openid'],'weixin_sales_user');
    		if(is_array($userInfo)&&!empty($userInfo['openid'])) {
    			if($userInfo['deal_state'] == 0) { //待审核
    				$msg = '您已提交过注册，正在审核中...';
    			} elseif ($userInfo['deal_state'] == 1) {  //用户已注册，且已通过审核
    				$msg = '您用户账号【'.$userInfo['sales_name'].'】注册过了，无须再注册';
    			} else {	//已注册，且审核未通过或者被软删除用户
    				$rowCount = model_user::update_weixin_register_user_info($data, $data['openid'],'weixin_sales_user'); //重新注册
    				if($rowCount > 0) {
    					self::smarty()->assign("result_class",'icon67_status');
			    		self::smarty()->assign("errMsg",'用户注册信息提交成功！');
			    		self::smarty()->assign("comment",'等待系统管理员审核！');
			    		self::smarty()->display('show_result.html');
			    		exit;
    				} else {
    					$msg = '服务器异常，请稍后重试！';
    				}    				
    			}
    			self::smarty()->assign("result_class",'icon67_status warn');
    			self::smarty()->assign("errMsg",'对不起，注册失败！');
    			self::smarty()->assign("comment",$msg);
    			self::smarty()->display('show_result.html');
    			exit;
    		}
    		self::smarty()->display('error/wx_error.html'); exit;
    	}
    }
}
